﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace DreamCMS.WebCore.Common
{
    /// <summary>
    /// 请求辅助类，处理和获取与客户请求相关的数据
    /// </summary>
    public class RequestHelper
    {
        private static string preFix = ConfigHelper.SitePrefix;
        /// <summary>
        /// 获取用户请求URL的参数
        /// </summary>
        /// <param name="queryName">参数名</param>
        /// <param name="isLower">是否转换为小写</param>
        /// <returns>参数名对应的参数值</returns>
        public static string GetQueryString(string queryName, bool isLower)
        {
            string rel = null;
            rel = HttpContext.Current.Request.QueryString[queryName];
            if (rel == null)
            {
                rel = string.Empty;
            }
            if (isLower)
            {
                rel = rel.ToLower();
            }
            rel = StringHelper.UrlDecode(rel);//为了保险起见，对参数进行url解码
            return rel;
        }
        /// <summary>
        /// 属性：表示当前请求是否来自移动端
        /// </summary>
        public static bool IsMobile
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    HttpRequest req = HttpContext.Current.Request;
                    //string agent = req.UserAgent;
                    string accept = req.ServerVariables["HTTP_ACCEPT"];
                    if (!string.IsNullOrEmpty(accept) && (accept.IndexOf("wap")) > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
        }


        /// <summary>
        /// 获取整型请求参数，若没有则返回默认值
        /// </summary>
        /// <param name="queryName">待获取的URL参数名</param>
        /// <param name="defaultValue">获取失败后的默认值</param>
        /// <returns></returns>
        public static long QueryInt(string queryName, long defaultValue)
        {
            string getValue = HttpContext.Current.Request.QueryString[queryName];
            if (string.IsNullOrEmpty(getValue))
            {
                return defaultValue;
            }
            long rel = Convert.ToInt64(getValue);
            return rel;
        }



        /// <summary>
        /// 获取当前请求的全部URL-包含了域名之后的所有请求路径和文件名（其实为何不用Request.URL呢）
        /// </summary>
        public static string CurrentUrl
        {
            get
            {
                return "http://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl;
            }
        }

        /// <summary>
        /// 获取某个cookie 的value字符串值（适用于value是一个字符串值的情况）
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public static string GetCookieStr(string cookieName)
        {
            cookieName = preFix + cookieName;
            HttpCookieCollection cookieCollection = HttpContext.Current.Request.Cookies;
            if (cookieCollection != null && cookieCollection[cookieName] != null)
            {
                HttpCookie cookie = cookieCollection[cookieName];
                return StringHelper.UrlDecode(cookie.Value);
            }
            return string.Empty;
        }
        /// <summary>
        /// 获取某个cookie对象（用户可以拿到cookie对象再自己取数据）
        /// </summary>
        /// <param name="cookieName">cookie的名字，不需加前缀</param>
        /// <returns>cookie对象</returns>
        public static HttpCookie GetCookie(string cookieName)
        {
            cookieName = preFix + cookieName;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            return cookie;
        }

        /// <summary>
        ///获取当前请求的 客户端用户的IP地址
        /// </summary>
        public static string UserIP
        {
            get
            {
                return HttpContext.Current.Request.UserHostAddress;
            }
        }
        /// <summary>
        /// 获取当前请求的虚拟路径
        /// </summary>
        public static string Path
        {
            get
            {
                return HttpContext.Current.Request.Path;
            }
        }
        #region 获取该网站的当前访问主机名，例如：www.baidu.com
        /// <summary>
        /// 获取该网站域名的访问主机名。即例如：www.baidu.com
        /// </summary>
        public static string SiteHostName
        {
            get
            {
                string hn = string.Empty;
                hn = HttpContext.Current.Request.Url.Host;
                return hn;
            }
        }
        #endregion

        #region 获取站点当前访问的主域名URL,包含协议名称和末尾的/，不包含域名后续的路径和文件
        /// <summary>
        /// 站点URL，包含协议名称+域名+末尾的/
        /// URL不需要保存到字段，因为站点可能有多个URL，不同用户访问的URL可能不同，因此必须实时获取返回。
        /// </summary>
        public static string SiteUrl
        {
            get
            {
                string siteUrl = string.Empty;
                siteUrl = "http://" + RequestHelper.SiteHostName  + ConfigHelper.SiteRootPath;
                if (RequestHelper.Port != 80)
                {
                    siteUrl = "http://" + RequestHelper.SiteHostName + ":" + RequestHelper.Port + ConfigHelper.SiteRootPath;
                }
                return siteUrl;
            }
        }
        #endregion

        #region 当前访问的域名端口号
        public static long Port
        {
            get
            {
                long port = 0;
                port = HttpContext.Current.Request.Url.Port;
                return port;
            }
        }
        #endregion
    }
}
